package com.snake.springboot.authority.web.controller;

import com.snake.springboot.authority.api.model.VerifyImageModel;
import com.snake.springboot.authority.api.model.VerifyTextCodeModel;
import com.snake.springboot.authority.api.service.VerifyImageService;
import com.snake.springboot.authority.api.service.VerifyTextCodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.snake.common.result.ResponseResult;
import org.snake.common.result.ResponseResultFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 类 {@code VerifyImageController}  <br> .
 * <p>验证码图片验证控制器类
 * <p>创建时间：2024/11/29
 * @author yxt
 */
@Api(tags = "图片验证码")
@RestController
@RequestMapping("/verify")
public class VerifyImageController {

    private VerifyImageService verifyImageService;
    private VerifyTextCodeService verifyTextCodeService;

    @Autowired
    public VerifyImageController(VerifyImageService verifyImageService,VerifyTextCodeService verifyTextCodeService) {
        this.verifyImageService = verifyImageService;
        this.verifyTextCodeService = verifyTextCodeService;
    }

    @ApiOperation("获取校验图片")
    @GetMapping("/get_verify_image")
    public ResponseResult<VerifyImageModel> getVerifyImage() throws Exception {
        VerifyImageModel result = verifyImageService.createVerifyImage();
        return ResponseResultFactory.ucc("获取成功！", result);
    }

    @ApiOperation("获取文字校验图片")
    @GetMapping("/get_text_verify_image")
    public ResponseResult<VerifyTextCodeModel> getVerifyTextImage(){
        VerifyTextCodeModel result = verifyTextCodeService.createVerifyImage();
        return ResponseResultFactory.ucc("获取成功！", result);
    }

    @ApiOperation("校验图片")
    @PostMapping("/verify_image")
    public ResponseResult<Boolean> verifyImage(@RequestBody VerifyImageModel verifyCode) throws Exception {
        try {
            Boolean result = verifyImageService.checkVerifyImage(verifyCode);
            return ResponseResultFactory.ucc("校验成功！", result);
        }catch (Exception e){
            return ResponseResultFactory.ucc(e.getMessage(), false);
        }
    }

    @ApiOperation("校验文字校验码")
    @PostMapping("/verify_text_code")
    public ResponseResult<Boolean> verifyTextCode(@RequestBody VerifyTextCodeModel verifyCode){
        try {
            Boolean result = verifyTextCodeService.checkVerifyCode(verifyCode);
            return ResponseResultFactory.ucc("校验成功！", result);
        }catch (Exception e){
            return ResponseResultFactory.ucc(e.getMessage(), false);
        }
    }
}
